const baseUrl = '/partners';
export default {
    // 验证url
    isURL(str_url) {
        var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
            + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" // ftp的user@
            + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
            + "|" // 允许IP和DOMAIN（域名）
            + "([0-9a-z_!~*'()-]+\.)*" // 域名- www.
            + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名
            + "[a-z]{2,6})" // first level domain- .com or .museum
            + "(:[0-9]{1,4})?" // 端口- :80
            + "((/?)|" // a slash isn't required if there is no file name
            + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
        var re = new RegExp(strRegex);
        return re.test(str_url);
    },
    // 验证json格式
    isJSON(str) {
        if (typeof str == 'string') {
            try {
                var obj = JSON.parse(str);
                if (typeof obj == 'object' && obj && !Array.isArray(obj)) {
                    return true;
                } else {
                    return false;
                }
            } catch (e) {
                return false;
            }
        }
    },
    // 生成20位随机数
    s20(numcount) {
        let data = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
        let result = "";
        for (var i = 0; i < numcount; i++) { //产生20位就使i<20
            let r = Math.floor(Math.random() * 10); //16为数组里面数据的数量，目的是以此当下标取数组data里的值！
            result += data[r]; //输出20次随机数的同时，让rrr加20次，就是20位的随机字符串了。
        }
        return result;
    },
    // 转大写
    isAllUppercase(word) {
        if (word == null || word == undefined || word == '') {
            return false
        }
        for (var i = 1; i < word.length; i++) {
            if (word[0] >= 'a' && word[0] <= 'z') {
                if (word[i] < 'a' || word[i] > 'z') {
                    return false;
                }
            } else if (word[1] >= 'a' && word[1] <= 'z') {
                if (word[i] < 'a' || word[i] > 'z') {
                    return false;
                }
            } else {
                if (word[i] < 'A' || word[i] > 'Z') {
                    return false;
                }
            }
        }
        return true;
    },
    // 比较
    compare(propertyName) {
        return function (object1, object2) {
            var value1 = object1[propertyName];
            var value2 = object2[propertyName];
            if (value2 > value1) {
                return -1;
            }
            else if (value2 < value1) {
                return 1;
            }
            else {
                return 0;
            }
        }
    },
    // 名称脱敏
    namePrivate(name) {
        if (null != name && name != undefined) {
            return name.substring(0, 1) + '**'
        } else {
            return ''
        }
    },
    // 全为大写
    isAllUppercase(word) {
        for (var i = 1; i < word.length; i++) {
            if (word[0] >= 'a' && word[0] <= 'z') {
                if (word[i] < 'a' || word[i] > 'z') {
                    return false;
                }
            } else if (word[1] >= 'a' && word[1] <= 'z') {
                if (word[i] < 'a' || word[i] > 'z') {
                    return false;
                }
            } else {
                if (word[i] < 'A' || word[i] > 'Z') {
                    return false;
                }
            }
        }
        return true;
    },
    // 树状结构扁平化
    treeToArray(tree) {
        var res = [];
        for (const item of tree) {
            const { children, ...i } = item;
            if (children && children.length) {
                res = res.concat(this.treeToArray(children));
            }
            res.push(i);
        }
        return res;
    },
    getQueryVariable(query,variable) {
        // var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) { return pair[1]; }
        }
        return (false);
    }
}